package com.abc.tacos.bean;

import lombok.Data;
import org.hibernate.validator.constraints.CreditCardNumber;

import javax.persistence.*;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Kar
 * @version 1.0
 * @date 2022/4/16 11:53
 */
@Data
@Entity
@Table(name = "Taco_Order")
public class Order /*implements Serializable */{

    //private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private Date placedAt;
    @NotBlank(message = "name is required")
    private String name;
    @NotBlank(message = "street is required")
    private String street;
    @NotBlank(message = "city is required")
    private String city;
    @NotBlank(message = "State is required")
    private String state;
    @NotBlank(message = "zip code is required")
    private String zip;
    /**
     * @CreditCardNumber 信用卡验证
     */
    @CreditCardNumber(message = "Not a valid credit card number")
    private String ccNumber;
    @Pattern(regexp = "^(0[1-9]|1[0-2])([\\/])([1-9][0-9])$",
            message = "Must be formatted MM/YY")
    private String ccExpiration;
    /**
     * @Digits 验证 Number 和 String 的构成是否合法
     * @Digits(integer=,fraction=) 验证字符串是否是符合指定格式的数字，
     * interger指定整数精度，fraction指定小数精度。
     * CVV: Card Verification Value
     */
    @Digits(integer = 3, fraction = 0, message = "Invalid CVV")
    private String ccCVV;

    /**
     * 新增关联表的属性
     */
    @ManyToMany(targetEntity = Taco.class)
    private List<Taco> tacos = new ArrayList<>();

    /**
     * 多个订单对应一个用户。
     *
     */
    @ManyToOne
    private User user;

    public void addDesign(Taco design) {
        this.tacos.add(design);
    }

    /**
     * 会在Taco持久化时调用，设置当前时间
     */
    @PrePersist
    void placedAt() {
        this.placedAt = new Date();
    }
}
